layui.use(['form','jquery','layer','table','laydate'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var table = layui.table;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#createDate',
        type: 'datetime'
    });
    $(document).click(function () {
        parent.$('.tab').hide();
    });
    //刷新按钮
    $('#refresh').click(function () {
        location.reload()
    });
    //返回按钮
    $('#goBack').click(function () {
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if('部门管理' == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    //添加部门
    addDept = function addDept(){
        layer.open({
            type: 2,
            title: '添加部门信息',
            area: ['500px', '450px'],
            offset: 'auto',
            shade: 0.4,
            content: '/jurisdiction/jurisdictionManagement/departmentRightsManagement/dept-add-message.html',
            success:function (layero, index) {
                var childHtml = layero.find('iframe').contents();
                childHtml.find('#createDate').val(setTime());
            },
            end: function () {
                getAllDeptsByNameDateStatus();
            }
        });
    };
    //进入页面查询所有数据
    getAllDeptsByNameDateStatus();

    //搜索按钮
    form.on('submit(search)',function (obj) {
        getAllDeptsByNameDateStatus(obj.field);
        return false;
    })
    //根据部门名称、管理员名称、创建日期 模糊查询、状态 精准查询 部门信息
    function getAllDeptsByNameDateStatus(depts){
        $.ajax({
            url: '/SyDept/getAllDeptsByNameDateStatus',
            data: depts,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                deptsTableRender(data.data);
            }
        });
    }

    function deptsTableRender(data){
        table.render({
            elem: '#deptsTable',
            title: '部门数据管理列表',
            data: data,
            width: 1630,
            toolbar: '#button_ids',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                //limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox',fixed:'left'},
                {title:'序号',align:'center',width:224,templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:"deptName",title:"部门名称",align:'center',width:224},
                {field:"deptDescirbe",title:"部门描述",align:'center',width:224},
                {field:"userAccount",title:"创建人",align:'center',width:224},
                {field:"status",title:"状态",align:'center',width:224,templet:function(data){
                    if(data.status == 1){
                        return '<input type=\"checkbox\" value="'+data.id +'" lay-skin=\"switch\" lay-text=\"启用|停用"\ checked lay-filter="switchTest">';
                    }else if(data.status == 2){
                        return '<input type=\"checkbox\" value="'+data.id +'" lay-skin=\"switch\" lay-text=\"启用|停用"\ lay-filter="switchTest">';
                    }
                }},
                {field:"createDate",title:"创建时间",align:'center',width:226},
                {field:'',title:"操作",align:'center',width:226,toolbar: '#operation'}
            ]]
        });
    }
    table.on('tool(deptsTable)',function (obj) {
        if(obj.event == 'updateTable'){
            layer.open({
                type: 2,
                title: '修改部门信息',
                area: ['500px', '450px'],
                offset: 'auto',
                shade: 0.4,
                content: '/jurisdiction/jurisdictionManagement/departmentRightsManagement/dept-update-message.html',
                success:function (layero, index) {
                    var childHtml = layero.find('iframe').contents();
                    childHtml.find('#deptName').val(obj.data.deptName);
                    childHtml.find('#deptId').val(obj.data.id);
                    childHtml.find('#userAccount').val(obj.data.userAccount);
                    childHtml.find('#createDate').val(obj.data.createDate);
                    childHtml.find('#updateDate').val(setTime());
                    childHtml.find('#deptDescirbe').val(obj.data.deptDescirbe);
                },
                end: function () {
                    getAllDeptsByNameDateStatus();
                }
            })
        }else if(obj.event == 'deleteTable'){
            var layerIndex = layer.confirm('确定要删除"' + obj.data.deptName + '"么?', {
                btn: ['确定','取消'],
                icon: 3,
                title: '提示'
            }, function(){
                delCurrentDeptTable(obj.data.id);
                layer.close(layerIndex);
            }, function(){
                layer.close(layerIndex);
            });
        }
    });
    //更改部门状态
    form.on('switch(switchTest)',function (data) {
        if(data.elem.checked){
            updateDeptStatus(data.value,1);
        }else{
            updateDeptStatus(data.value,2);
        }
    });
    //更改部门信息
    function updateDeptMessage(param){
        $.ajax({
            url: '/SyDept/updateDeptMessage',
            data: param,
            type: 'post',
            dataType: 'json',
            success:function (data) {
                layer.msg(data.data);
                getAllDeptsByNameDateStatus();
            }
        });
    }
    function updateDeptStatus(id,status){
        $.ajax({
            url: '/SyDept/updateDeptStatus',
            data:{
                id: id,
                status: status,
            },
            dataType: 'json',
            type: 'post',
            success: function (data) {
                if(status == 1){
                    layer.msg('启用成功');
                }else if(status == 2){
                    layer.msg('停用成功');
                }
                getAllDeptsByNameDateStatus();
            }
        });
    }
    //删除当前部门
    function delCurrentDeptTable(id){
        $.ajax({
            url: '/SyDept/delCurrentDeptTable',
            data:{
                id: id,
                status: status
            },
            dataType: 'json',
            type: 'post',
            success: function (data) {
                layer.msg(data.data);
                getAllDeptsByNameDateStatus();
            }
        });
    }
    //获取 年月日 时分秒 星期
    function setTime(){
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var today = date.getDate();
        var week = date.getDay();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        //当小时小于10时进行补零
        var currentTime = zeroFill(year) + '-' + zeroFill(month) + '-' + zeroFill(today) +  ' ' + zeroFill(hour) + ':' + zeroFill(minute) + ':' + zeroFill(second);
        return currentTime;
    }
    //补零
    function zeroFill(time){
        if(time < 10){
            time = '0' + time;
        }
        return time;
    }


})